{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#事件循环+回调（驱动生成器）+epoll(IO多路复用)\n",
    "#asyncio是python用于解决异步io编程的一整套解决方案\n",
    "#tornado、gevent、twisted（scrapy， django channels）\n",
    "#torando(实现web服务器)， django+flask(uwsgi, gunicorn+nginx)\n",
    "#tornado可以直接部署， nginx+tornado\n",
    "\n",
    "#使用asyncio\n",
    "# import asyncio\n",
    "# import time\n",
    "# async def get_html(url):\n",
    "#     print(\"start get url\")\n",
    "#     await asyncio.sleep(2)\n",
    "#     print(\"end get url\")\n",
    "#\n",
    "# if __name__ == \"__main__\":\n",
    "#     start_time = time.time()\n",
    "#     loop = asyncio.get_event_loop()\n",
    "#     tasks = [get_html(\"http://www.imooc.com\") for i in range(10)]\n",
    "#     loop.run_until_complete(asyncio.wait(tasks))\n",
    "#     print(time.time()-start_time)\n",
    "\n",
    "#获取协程的返回值\n",
    "# import asyncio\n",
    "# import time\n",
    "# from functools import partial\n",
    "# async def get_html(url):\n",
    "#     print(\"start get url\")\n",
    "#     await asyncio.sleep(2)\n",
    "#     return \"bobby\"\n",
    "#\n",
    "# def callback(url, future):\n",
    "#     print(url)\n",
    "#     print(\"send email to bobby\")\n",
    "#\n",
    "# if __name__ == \"__main__\":\n",
    "#     start_time = time.time()\n",
    "#     loop = asyncio.get_event_loop()\n",
    "#     # get_future = asyncio.ensure_future(get_html(\"http://www.imooc.com\"))\n",
    "#     task = loop.create_task(get_html(\"http://www.imooc.com\"))\n",
    "#     task.add_done_callback(partial(callback, \"http://www.imooc.com\"))\n",
    "#     loop.run_until_complete(task)\n",
    "#     print(task.result())\n",
    "#wait 和 gather\n",
    "import asyncio\n",
    "import time\n",
    "async def get_html(url):\n",
    "    print(\"start get url\")\n",
    "    await asyncio.sleep(2)\n",
    "    print(\"end get url\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    start_time = time.time()\n",
    "    loop = asyncio.get_event_loop()\n",
    "    tasks = [get_html(\"http://www.imooc.com\") for i in range(10)]\n",
    "    # loop.run_until_complete(asyncio.gather(*tasks))\n",
    "    # print(time.time()-start_time)\n",
    "\n",
    "    #gather和wait的区别\n",
    "    #gather更加high-level\n",
    "    group1 = [get_html(\"http://projectsedu.com\") for i in range(2)]\n",
    "    group2 = [get_html(\"http://www.imooc.com\") for i in range(2)]\n",
    "    group1 = asyncio.gather(*group1)\n",
    "    group2 = asyncio.gather(*group2)\n",
    "    group2.cancel()\n",
    "    loop.run_until_complete(asyncio.gather(group1, group2))\n",
    "    print(time.time() - start_time)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
